<?php

require_once('BaseModel.php');

class Grupos extends BaseModel{
	protected $_name =  "usuarios_niveis";
	/**
	 * @return the $_name
	 */
	public function getName() {
		return $this->_name;
	}
	
	public function getArvorePrivilegios( $nivel , $pai = NULL ){
		$select = $this->select()->where('status != ?', 2);
		if( $nivel ){
			$select->where('nivel = ? ', $nivel );
		} else if( $pai ){
			$select->where('pai = ? ', $pai );
		}

		$data = $this->fetchAll( $select );

		$privileges = array();
		if($data->count()){
			foreach($data->toArray() as $row ){
				$lv = $this->getArvorePrivilegios( NULL , $row['nivel'] );
				
				$privileges[$row['nivel']] = array('name'=>$row['nome'], 'childs'=>$lv );
			}
		}
		if( $privileges )
			return $privileges;
		else return NULL;
	}
}